# ==================================
#
#  Code for replicating:
# "Positioning Under Alternative Electoral Systems: Evidence From Japanese Candidate Election Manifestos"
#  Amy Catalinac, NYU
#
# ==================================




# ==================================
# Identifying pairs of LDP cands who ran in same district in 1993
# and who ran again (in different districts) in 1996
# (Figure 4 in paper)

load("covars_ideal_points.Rdata") # saved as ideal, sep_theta in 5th column

# Identify all cands from the same party who ran in the same district in 1993, 
# and calculate their absolute distances from each other

dat1993 <- ideal[ideal$year==1993,]
pku <- paste(dat1993$pty,dat1993$ku, sep="_")
dat1993$pku <- pku
multi <- names(which(table(pku)>1))
dat1993_multi <- dat1993[dat1993$pku%in%multi,]
out.mat <- data.frame(year=NA, ku=NA, party=NA, pku=NA, cand1=as.character(NA), cand2=as.character(NA), 
                      ab.dist=NA)
re <- function(x){rep(unique(x), trow)}
for(i in 1:length(multi)){
  sub <- dat1993_multi[dat1993_multi$pku==multi[i],]
  combo <- combn(sub$���O,2)
  tcombo <- t(combo)
  trow <- nrow(tcombo)
  dists <- c(dist(sub$sep_theta))
  out<- cbind(re(sub$year), re(sub$ku), re(sub$pty), re(sub$pku), as.character(tcombo[,1]), as.character(tcombo[,2]),dists)
  colnames(out) <- colnames(out.mat)
  out.mat <- rbind(out.mat, out)
}
dat93 <- out.mat[-c(1),]
save(dat93, file="abs_dists_pairs93.Rdata")

# Identify all cands from the same party in 1996
# and calculate their absolute distances from each other

dat1996 <- ideal[ideal$year==1996,]
table(dat1996$pty)
multi <- names(which(table(dat1996$pty)>1))
dat1996_multi <- dat1996[dat1996$pty%in%multi,]
table(dat1996_multi$pty)
out.mat <- data.frame(year=NA, party=NA, cand1=as.character(NA), cand2=as.character(NA), ab.dist=NA)
re <- function(x){rep(unique(x), trow)}
for(i in 1:length(multi)){
  sub <- dat1996_multi[dat1996_multi$pty==multi[i],]
  combo <- combn(sub$���O,2)
  tcombo <- t(combo)
  trow <- nrow(tcombo)
  dists <- c(dist(sub$sep_theta))
  out<- cbind(re(sub$year), re(sub$pty), as.character(tcombo[,1]), as.character(tcombo[,2]),dists)
  colnames(out) <- colnames(out.mat)
  out.mat <- rbind(out.mat, out)
}
dat96 <- out.mat[-c(1),]
save(dat96, file="abs_dists_pairs96.Rdata")

rm(list=ls())

# Identify the pairwise combinations in 1993 who also ran in 1996:
load("abs_dists_pairs93.Rdata")
dyad93 <- paste(dat93$cand1, dat93$cand2, sep="_")
dat93$dyad93 <- dyad93
load("abs_dists_pairs96.Rdata")
dyad96_a <- paste(dat96$cand1, dat96$cand2, sep="_")
dyad96_b <- paste(dat96$cand2, dat96$cand1, sep="_")
dat96$dyad96_a <- dyad96_a
dat96$dyad96_b <- dyad96_b
ma <- match(dat93$dyad93,dat96$dyad96_a)
mb <- match(dat93$dyad93,dat96$dyad96_b)
pairs.present<- c(which(!is.na(ma)), which(!is.na(mb)))
present9396 <- dat93[pairs.present,]
ref96 <- dat96[as.numeric(na.omit(c(ma, mb))),]
colnames(ref96)[5] <- "ab.dist96"
all.together <- cbind(present9396, ref96)
nrow(all.together) # 64 pairs of LDP cands

all.together$ab.dist <- as.numeric(all.together$ab.dist)
all.together$ab.dist96 <- as.numeric(all.together$ab.dist96)

# For each pair, is absolute distance in 1993 greater than in 1996?
allt <- all.together[,c(7)]>all.together[,c(13)]
table(allt)
sum(allt)/nrow(all.together)

# Difference in means test
t.test(all.together$ab.dist, all.together$ab.dist96, paired=T)

save(all.together, file="pairwise_abs_dists_9396.Rdata")

rm(list=ls())

